草庐IT

c++ - Visual C++ volatile

全部标签

java - 如果您将对象分配给最终字段,其他线程是否会看到该对象的非最终/非 volatile 字段的先前更新?

阅读Java语言规范时,我发现了这段关于final字段的摘录:Theusagemodelforfinalfieldsisasimpleone:Setthefinalfieldsforanobjectinthatobject'sconstructor;anddonotwriteareferencetotheobjectbeingconstructedinaplacewhereanotherthreadcanseeitbeforetheobject'sconstructorisfinished.Ifthisisfollowed,thenwhentheobjectisseenbyanothe

java - 如何在没有 volatile 的情况下打破双重检查锁定

我知道doublechecklockingwithoutvolatilevariableisnotsafe基于此链接http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.htmlclassFoo{privateHelperhelper=null;publicHelpergetHelper(){if(helper==null){synchronized(this){if(helper==null){helper=newHelper();}}}returnhelper;}}我想在家里的电脑上模拟这种情况。我有标准

java - volatile 关键字和内存一致性错误

在oracleJava文档中locatedhere,下面说:Atomicactionscannotbeinterleaved,sotheycanbeusedwithoutfearofthreadinterference.However,thisdoesnoteliminateallneedtosynchronizeatomicactions,becausememoryconsistencyerrorsarestillpossible.Usingvolatilevariablesreducestheriskofmemoryconsistencyerrors,becauseanywrite

java - 完全 volatile 可见性保证

我正在阅读一篇关于JavaVolatile关键字的文章,遇到了一些问题。clickherepublicclassMyClass{privateintyears;privateintmonthsprivatevolatileintdays;publicvoidupdate(intyears,intmonths,intdays){this.years=years;this.months=months;this.days=days;}}udpate()方法写入了三个变量,其中只有days是volatile的。完整的volatile可见性保证意味着,当一个值被写入days时,线程可见的所有变量

java - volatile 变量并刷新到主内存/从主内存读取

官方注释说,那Writingtoavolatilefieldhasthesamememoryeffectasamonitorrelease,andreadingfromavolatilefieldhasthesamememoryeffectasamonitoracquire.和Effectively,thesemanticsofvolatilehavebeenstrengthenedsubstantially,almosttothelevelofsynchronization.Eachreadorwriteofavolatilefieldactslike"half"asynchroni

java - 一个线程中对非 volatile 成员变量的赋值是否保证在另一个线程中可见?

考虑下面的Java示例。请注意,两个类成员变量都未声明为volatile。如果我正确理解内存模型和“先于发生”规则,Java实现可以优化run()方法,使其永远运行,即使另一个线程调用stopNow()方法。发生这种情况是因为run()方法中没有任何内容强制线程多次读取stop的值。那是对的吗?如果不是,为什么不呢?classExampleimplementsRunnable{booleanstop=false;intvalue=0;publicvoidstopNow(){stop=true;}publicintgetValue(){returnvalue;}@Overridepubl

java,线程何时(以及多长时间)可以缓存非 volatile 变量的值?

来自这篇文章:http://www.javamex.com/tutorials/synchronization_volatile_typical_use.shtmlpublicclassStoppableTaskextendsThread{privatevolatilebooleanpleaseStop;publicvoidrun(){while(!pleaseStop){//dosomestuff...}}publicvoidtellMeToStop(){pleaseStop=true;}}Ifthevariablewerenotdeclaredvolatile(andwithout

java - 混合 volatile 和 synchronized 作为读写锁

考虑一个原始类型变量,有很多线程读取和一些线程写入,下面的代码能正常工作吗?如果会,它提供的性能是否优于1)。在所有方法上声明同步;2).使用显式ReadWriteLock?这是一个常见的模式吗?如果不是,这种情况通常使用什么模式?目前这对我来说效果很好,但我觉得同时使用volatile和synchronized有点多余。privatevolatileintvalue=1;publicvoidfunc1(){if(value==1){//dosomething}}publicvoidfunc2(){if(value==2){//dosomething}}publicvoidfunc3(

java - volatile + 不可变持有者对象 = 线程安全?

我有一个来自“javaconcurrencypratique”一书的例子,他说volatile和不可变的持有者对象提供了线程安全性。但是我不明白书上给出的例子。代码如下:publicclassVolatileCachedFactorizerextendsGenericServletimplementsServlet{privatevolatileOneValueCachecache=newOneValueCache(null,null);publicvoidservice(ServletRequestreq,ServletResponseresp){BigIntegeri=extrac

java - Java中如何正确使用volatile

我正在研究JavaThread,当我分析以下修改自Example8.3.1.4的代码时,关键字volatile让我感到困惑publicclassVolatile{publicstaticvoidmain(String[]args){MyRunnable1myRunnable1=newMyRunnable1();MyRunnable2myRunnable2=newMyRunnable2();Threadt1=newThread(myRunnable1);Threadt2=newThread(myRunnable2);t1.start();t2.start();}}classMyRunna